package com.szrt.ch04.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.szrt.ch04.dao.UserDao;
import com.szrt.ch04.dto.PageDTO;
import com.szrt.ch04.entity.UserEntity;
import com.szrt.ch04.utils.db.DBUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public List<UserEntity> findWithPage(PageDTO page) throws Exception {
		
		
		// 0 2         
		// 2 2
		
		Connection connection = DBUtil.getConnectionMySQL();
		
		String sql ="select ID as id , USERNAME as phone,PASSWORD as  password from tmall_user_t limit ?,? ";
		PreparedStatement ps = connection.prepareStatement(sql );
		
		ps.setInt(1, (page.getCurrentPage() -1) * page.getPageSize());
		
		ps.setInt(2, page.getPageSize() );
		
		
		System.out.println(ps);
		
		ResultSet rs = ps.executeQuery();
		
		List<UserEntity> list = new ArrayList<UserEntity>();
		while(rs.next()){
			
			UserEntity entity = new UserEntity();
			entity.setId(rs.getInt("id"));
			entity.setPhone(rs.getString("phone"));
			entity.setPassword(rs.getString("password"));
			
			list.add(entity);
			
		}
		DBUtil.closeResource(rs, ps, connection);
		return list;
	}

}
